package service;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDao;
import daoImp.UserDaoImp;
import model.User;


public class Admin_AddUserServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");//传值编码
		response.setContentType("text/html;charset=UTF-8");//设置传输编码
		PrintWriter out = response.getWriter();
		this.doPost(request, response);
		out.flush();
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");//传值编码
		response.setContentType("text/html;charset=UTF-8");//设置传输编码
		PrintWriter out = response.getWriter();
		String uname = request.getParameter("uname");
		String upassword = request.getParameter("upassword");
		User user = new User();
		user.setUname(uname);
		user.setUpassword(upassword);
		UserDao dao = new UserDaoImp();
		if (!dao.isExist(uname)){
			dao.addUser(user);
			//request.getRequestDispatcher("/user/showAllUsers.jsp").forward(request,response);
			response.sendRedirect("Admin_ShowAllUserServlet");//重定向到显示全部的Servlet中
		}
		else{
			System.out.println(user.getUname() + " fail register");
			request.setAttribute("msg", "用户名"+uname+"已存在");
			response.sendRedirect(request.getContextPath()+"/Admin_addUser.jsp");
			//request.getRequestDispatcher("/user/addUser.jsp").forward(request,response);
		}
		out.flush();
		out.close();
	}
}
